import { _searchData, _searchResult, _goodsList } from "../../service/api";

Page({
  data: {
    keyword: "",
    searchStatus: false, // 搜索状态
    goodsList: [],
    searchResult: [], // 搜索结果
    historyKeywords: [], // 历史记录
    defaultKeyword: {}, // 默认搜索
    hotKeywordList: [], // 热门搜索
    page: 1,
    size: 20,
    count: 0,
    time: null, // 定时器
  },
  onLoad: function (options) {
    this.initData();
  },
  onShow() {
    const historyList = wx.getStorageSync("historyKeywords") || [];
    this.setData({ historyKeywords: historyList });
  },
  // 初始化数据
  async initData() {
    const res = await _searchData();
    this.setData({
      defaultKeyword: res.defaultKeyword[0],
      hotKeywordList: res.hotKeywordList,
    });
  },
  // 输入触发
  handleInputChange(e) {
    const { value } = e.detail;
    this.setData({ keyword: value, searchStatus: false });
    clearTimeout(this.data.time);
    this.data.time = setTimeout(() => {
      this.getSearchResult();
    }, 500);
  },
  // 获取搜索内容
  async getSearchResult() {
    const res = await _searchResult(this.data.keyword);
    let flag = res.length ? false : true;
    this.setData({ searchResult: res, searchStatus: flag });
  },
  // 清除搜索内容
  clearKeyword() {
    this.setData({ keyword: "", searchStatus: false, goodsList: [] });
  },
  // 键盘确认、搜索按钮触发
  handleConfirm(e) {
    let value = e.detail.value;
    if (e.target.dataset.type === "button") {
      value = this.data.keyword;
    }
    if (!value.trim()) {
      return wx.showToast({
        title: "请输入搜索内容",
        icon: "error",
        duration: 1000,
        mask: true,
      });
    }
    this.storageKeyword(value);
    this.getGoodsList(value);
  },
  // 点击历史记录、热门搜索、搜索列表触发
  keywordTap(e) {
    const { keyword } = e.target.dataset;
    this.storageKeyword(keyword);
    this.getGoodsList(keyword);
  },
  // tab栏切换
  hdnaleChangeSort(e) {
    const { type, order } = e.detail;
    this.getGoodsList(this.data.keyword, type, order);
  },
  // 加载更多
  async handleReachBottom(e) {
    const { page, size, type, order } = e.detail;
    const result = await _goodsList({ keyword: this.data.keyword, page, size, sort: type, order });
    this.setData({
      searchStatus: true,
      goodsList: [...this.data.goodsList, ...result.data],
    });
  },
  // 获取商品列表
  async getGoodsList(value, sort = "default", order = "desc") {
    wx.showLoading({ title: "加载中..." });
    this.setData({ keyword: value, page: 1 });
    const { keyword, page, size } = this.data;
    const res = await _goodsList({ keyword, page, size, sort, order });
    this.setData({ searchStatus: true, goodsList: res.data, count: res.count });
    wx.hideLoading();
  },
  // 存储搜索记录
  storageKeyword(keyword) {
    let { historyKeywords } = this.data;
    if (historyKeywords.length > 0) {
      let flag = historyKeywords.includes(keyword) ? true : false;
      if (!flag) {
        historyKeywords.unshift(keyword);
      }
    } else {
      historyKeywords.unshift(keyword);
    }
    this.setData({ historyKeywords: historyKeywords });
    wx.setStorageSync("historyKeywords", historyKeywords);
  },
  // 清空历史记录
  handleClearHistory() {
    wx.removeStorageSync("historyKeywords");
    this.setData({ historyKeywords: [] });
  },
});
